package org.oaix.utils;

import java.security.MessageDigest;

public class MD5Util {
    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }



    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };



    public static String getMD5(String val) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(val.getBytes("utf-8"));
        //加密
        byte[] m = md5.digest();
        return getString(m);
    }

    private static String getString(byte[] b) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            int a = b[i];
            if (a < 0)
                a += 256;
            if (a < 16)
                buf.append("0");
            buf.append(Integer.toHexString(a));

        }
        return buf.toString();//32位
        // 或者
//        return buf.toString().substring(8, 24);// 16位
    }
    //signValue
    public static void main(String[] args) throws Exception {
        StringBuilder sb = new StringBuilder();
        String custCode  = "cust000000001";
        String timestrap = String.valueOf(System.currentTimeMillis());
        System.out.println(timestrap);
        String signType = "MD5";
        String fileName = "fj.jepg";
        String src = "lsdfhjkjasdhfkjsdahfkjhsadkf";
        sb.append(custCode);
        sb.append(timestrap);
        sb.append(signType);
        sb.append(fileName);
        sb.append(src);
        String s = getMD5(sb.toString());
        String s2 = MD5(sb.toString());
        System.out.println(s);
        System.out.println(s2);


    }
    public static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                             'A', 'B', 'C', 'D', 'E', 'F' };

        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();

            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
//    public static void main(String[] args) throws Exception {
//        StringBuilder sb = new StringBuilder();
//        String svcName = "CHCExeInfoRevService";
//        String entLicenseNum = "123456";
//        String custCode  = "cust000000001";
//        String thirdId = "123456";
//        String timestrap = String.valueOf(System.currentTimeMillis());
//        System.out.println(timestrap);
//        String signType = "MD5";
//        String fileName = "fj";
//        String breakDetail = "a";
//        String breakDateTime = "2022-02-02 12:00:00";
//        String breakProof = "123456";
//        String src = "lsdfhjkjasdhfkjsdahfkjhsadkf";
//        sb.append(svcName);
//        sb.append(custCode);
//        sb.append(entLicenseNum);
//        sb.append(timestrap);
//        sb.append(signType);
//        sb.append(breakDetail);
//        sb.append(breakProof);
//        sb.append(src);
//        String s = MD5Encode(sb.toString(),null);
//        System.out.println(s);
//    }
}
